#!/bin/bash

# ================================================================
# 首页文件迁移脚本
# 从旧项目 site/pc 迁移首页相关文件到新项目 newsite/trading-center-front
# ================================================================

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 路径定义
OLD_PROJECT="/Users/kevin/项目文件夹/交易平台/site/pc"
NEW_PROJECT="/Users/kevin/项目文件夹/交易平台/newsite/trading-center-front"
BACKUP_DIR="/Users/kevin/项目文件夹/交易平台/newsite/说明:脚本/backup/$(date +%Y%m%d_%H%M%S)"

echo "================================================================"
echo "              首页文件迁移工具"
echo "================================================================"
echo ""
echo -e "${BLUE}旧项目路径:${NC} $OLD_PROJECT"
echo -e "${BLUE}新项目路径:${NC} $NEW_PROJECT"
echo -e "${BLUE}备份目录:${NC} $BACKUP_DIR"
echo ""

# 创建备份目录结构
mkdir -p "$BACKUP_DIR/components"
mkdir -p "$BACKUP_DIR/pages"
mkdir -p "$BACKUP_DIR/composables"
mkdir -p "$BACKUP_DIR/assets"

echo "----------------------------------------------------------------"
echo "第1步：备份新项目中将要被覆盖的文件"
echo "----------------------------------------------------------------"

# 需要备份的组件列表（两个项目都有的）
SHARED_COMPONENTS=(
    "ChangxinExpress.vue"
    "ExportProducts.vue"
    "FindGoods.vue"
    "HotProducts.vue"
    "IndustryActivities.vue"
    "LocalProducts.vue"
    "TechExchange.vue"
)

for component in "${SHARED_COMPONENTS[@]}"; do
    if [ -f "$NEW_PROJECT/components/$component" ]; then
        echo -e "${YELLOW}备份:${NC} components/$component"
        cp "$NEW_PROJECT/components/$component" "$BACKUP_DIR/components/"
    fi
done

# 备份 HomeIndex.vue
if [ -f "$NEW_PROJECT/components/HomeIndex.vue" ]; then
    echo -e "${YELLOW}备份:${NC} components/HomeIndex.vue"
    cp "$NEW_PROJECT/components/HomeIndex.vue" "$BACKUP_DIR/components/"
fi

# 备份页面文件
if [ -f "$NEW_PROJECT/pages/home/index.vue" ]; then
    echo -e "${YELLOW}备份:${NC} pages/home/index.vue"
    cp "$NEW_PROJECT/pages/home/index.vue" "$BACKUP_DIR/pages/"
fi

if [ -f "$NEW_PROJECT/pages/index.vue" ]; then
    echo -e "${YELLOW}备份:${NC} pages/index.vue"
    cp "$NEW_PROJECT/pages/index.vue" "$BACKUP_DIR/pages/"
fi

echo ""
echo "----------------------------------------------------------------"
echo "第2步：复制旧项目独有的组件（新增文件）"
echo "----------------------------------------------------------------"

# 旧项目独有的组件
UNIQUE_COMPONENTS=(
    "CategoryMenu.vue"
    "CategoryCarousel.vue"
    "HomeBanner.vue"
    "NoticePanel.vue"
    "MarketBazaar.vue"
    "MoreSections.vue"
    "DataStats.vue"
    "PlatformServices.vue"
)

for component in "${UNIQUE_COMPONENTS[@]}"; do
    if [ -f "$OLD_PROJECT/components/home/$component" ]; then
        echo -e "${GREEN}新增:${NC} components/$component"
        cp "$OLD_PROJECT/components/home/$component" "$NEW_PROJECT/components/"
    else
        echo -e "${RED}未找到:${NC} $OLD_PROJECT/components/home/$component"
    fi
done

echo ""
echo "----------------------------------------------------------------"
echo "第3步：覆盖共享的组件（已备份）"
echo "----------------------------------------------------------------"

for component in "${SHARED_COMPONENTS[@]}"; do
    if [ -f "$OLD_PROJECT/components/home/$component" ]; then
        echo -e "${BLUE}覆盖:${NC} components/$component"
        cp "$OLD_PROJECT/components/home/$component" "$NEW_PROJECT/components/"
    fi
done

echo ""
echo "----------------------------------------------------------------"
echo "第4步：复制屏幕适配相关的 composables"
echo "----------------------------------------------------------------"

# 备份现有的 composables
if [ -d "$NEW_PROJECT/composables" ]; then
    cp -r "$NEW_PROJECT/composables"/* "$BACKUP_DIR/composables/" 2>/dev/null || true
fi

# 复制屏幕适配相关的 composables
if [ -f "$OLD_PROJECT/composables/useScreenAdaptation.ts" ]; then
    echo -e "${GREEN}复制:${NC} composables/useScreenAdaptation.ts"
    cp "$OLD_PROJECT/composables/useScreenAdaptation.ts" "$NEW_PROJECT/composables/"
fi

if [ -f "$OLD_PROJECT/composables/useZoomAdjustment.ts" ]; then
    echo -e "${GREEN}复制:${NC} composables/useZoomAdjustment.ts"
    cp "$OLD_PROJECT/composables/useZoomAdjustment.ts" "$NEW_PROJECT/composables/"
fi

echo ""
echo "----------------------------------------------------------------"
echo "第5步：复制样式文件"
echo "----------------------------------------------------------------"

# 备份现有样式
if [ -d "$NEW_PROJECT/assets/styles" ]; then
    cp -r "$NEW_PROJECT/assets/styles" "$BACKUP_DIR/assets/" 2>/dev/null || true
fi

# 创建styles目录（如果不存在）
mkdir -p "$NEW_PROJECT/assets/styles"

# 复制样式文件
if [ -d "$OLD_PROJECT/assets/styles" ]; then
    echo -e "${GREEN}复制:${NC} assets/styles/"
    cp -r "$OLD_PROJECT/assets/styles"/* "$NEW_PROJECT/assets/styles/"
fi

echo ""
echo "----------------------------------------------------------------"
echo "第6步：创建迁移报告"
echo "----------------------------------------------------------------"

# 创建迁移报告
REPORT_FILE="$BACKUP_DIR/迁移报告.md"

cat > "$REPORT_FILE" << EOF
# 首页文件迁移报告

**迁移时间**: $(date '+%Y-%m-%d %H:%M:%S')

## 📋 迁移概览

### 新增的组件 (8个)
EOF

for component in "${UNIQUE_COMPONENTS[@]}"; do
    if [ -f "$NEW_PROJECT/components/$component" ]; then
        echo "- ✅ $component" >> "$REPORT_FILE"
    else
        echo "- ❌ $component (失败)" >> "$REPORT_FILE"
    fi
done

cat >> "$REPORT_FILE" << EOF

### 覆盖的组件 (7个，已备份)
EOF

for component in "${SHARED_COMPONENTS[@]}"; do
    if [ -f "$NEW_PROJECT/components/$component" ]; then
        echo "- ✅ $component" >> "$REPORT_FILE"
    fi
done

cat >> "$REPORT_FILE" << EOF

### Composables
- useScreenAdaptation.ts - 屏幕适配功能
- useZoomAdjustment.ts - 缩放调整功能

### 样式文件
- assets/styles/ - 完整的样式系统

## 📦 备份位置

所有被覆盖的文件已备份到:
\`\`\`
$BACKUP_DIR
\`\`\`

## ⚠️ 注意事项

1. **组件导入**: 新增的组件需要在页面中导入和使用
2. **样式依赖**: mixins.scss 需要在全局配置中引入
3. **Composables**: useScreenAdaptation 需要在页面中正确引用
4. **API调用**: 确保所有组件的API调用路径正确

## 🔄 下一步操作

1. 检查新增组件的导入
2. 测试屏幕适配功能
3. 验证所有组件的数据加载
4. 调整样式以适配新项目

## 📝 文件清单

### 备份的文件
\`\`\`
$(ls -la "$BACKUP_DIR/components" 2>/dev/null | grep "\.vue" | awk '{print $9}' | sed 's/^/- components\//')
\`\`\`

### 新增的文件
\`\`\`
$(for c in "${UNIQUE_COMPONENTS[@]}"; do echo "- components/$c"; done)
\`\`\`

---

**迁移完成** ✅
EOF

echo ""
echo "================================================================"
echo "                    迁移完成！"
echo "================================================================"
echo ""
echo -e "${GREEN}✅ 新增组件:${NC} ${#UNIQUE_COMPONENTS[@]} 个"
echo -e "${BLUE}📝 覆盖组件:${NC} ${#SHARED_COMPONENTS[@]} 个（已备份）"
echo -e "${YELLOW}📦 备份目录:${NC} $BACKUP_DIR"
echo ""
echo -e "${BLUE}📄 迁移报告:${NC} $REPORT_FILE"
echo ""
echo "请查看迁移报告了解详细信息！"
echo ""

# 显示报告内容
cat "$REPORT_FILE"

